<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/8
 * Time: 10:24
 */
namespace app\api\model;

use think\Db;
use think\Model;

class BusinessModel extends Model
{
    /**
     * 商家列表
     * @param $page int 当前页
     * @param $limit int 每页显示的数量
     * @return array
     */
    public function getList($page,$limit=10){
        $offset = ($page-1)*$limit;
        $info = Db::name('business')->field(['id','name','cover_img','desc','logo'])->where(['is_effect'=>1])->order(['sort'=>'DESC'])->limit($offset,$limit)->select();
        dealImg($info,'logo');
        dealImg($info,'cover_img');
        $count = Db::name('business')->where(['is_effect'=>1])->count();
        $all_page = ceil($count/$limit);
        $data['list'] = $info;
        $data['page'] = $page;
        $data['all_page'] = $all_page;
        return $data;
    }

    /**
     * 搜索列表
     * @param $key string 搜索关键字
     * @param $type int 筛选类型id
     * @return array
     */
    public function searchList($key){
        $where['is_effect'] = 1;
        //获取商家信息
        $business = Db::name('business')->field(['id','name','cover_img','desc','logo'])->where('name','like','%'.$key.'%')->where($where)->select();
        //获取商品信息
        $product = Db::name('business_product')->where('name','like','%'.$key.'%')->where($where)->select();
        if(!empty($business)){
            foreach($business as $k=>$v){
                dealImg($business[$k],'cover_img');
                dealImg($business[$k],'logo');
                $business[$k]['is_business'] = 1;
            }
        }
        if(!empty($product)){
            foreach($product as $k=>$v){
                $product[$k]['is_business'] = 0;
            }
        }
        $data = array_merge($business,$product);
        return $data;
    }
}